package logic

import (
	"context"
	"fmt"
	"phone/models/mysql"
	"phone/models/redis"

	"phone/phonerpc/internal/svc"
	"phone/phonerpc/phonerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *LoginLogic) Login(in *phonerpc.LoginRequest) (*phonerpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.PhoneUser
	mysql.DB.Where("account=?", in.Account).Find(&user)
	result, _ := redis.Rdb.Get(l.ctx, "graphic"+in.Account).Result()
	if result != in.Graphic {
		return nil, fmt.Errorf("图形验证错误")
	}
	return &phonerpc.LoginResponse{
		Id: int64(user.ID),
	}, nil
}
